/*
 * Copyright 2013 HVTTGROUP
 * http://hvttgroup.com
 *
 */
package com.hvtt.web.controllerUtils;

import com.hvtt.web.controller.Sys_NguoiDungController;

import com.hvtt.web.models.Sys_nguoidung;
import com.hvtt.web.utilities.SocialAPIWrapper;
import com.hvtt.web.utilities.Utilities;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ComponentSystemEvent;
import org.apache.commons.lang3.text.WordUtils;
import org.json.simple.JSONObject;
import org.primefaces.context.RequestContext;

/**
 *
 * @author Nguyễn Hoàng Danh
 */
@ManagedBean
@SessionScoped
public class UtilsController implements Serializable {
    
    private String msgImage = "/Administrator/images/warning.png";
    private String message = "Unknown message";
    private String addThis = "<!-- AddThis Button BEGIN -->\n"
            + "<div class=\"addthis_toolbox addthis_floating_style addthis_counter_style\" style=\"left:1px;top:130px;background:#F4F4F4;\">\n"
            + "<a class=\"addthis_button_facebook_like\" fb:like:layout=\"box_count\"></a>\n"
            + "<a class=\"addthis_button_tweet\" tw:count=\"vertical\"></a>\n"
            + "<a class=\"addthis_button_google_plusone\" g:plusone:size=\"tall\"></a>\n"
            + "<a class=\"addthis_counter\"></a>\n"
            + "</div>\n"
            + "<script type=\"text/javascript\" src=\"//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-5051f0a44eb52573\"></script>\n"
            + "<!-- AddThis Button END -->";
    
    public String getAddThis() {
        return addThis;
    }
    
    public void setAddThis(String addThis) {
        this.addThis = addThis;
    }
    
    public UtilsController() {
    }
    
    public void unsupportMethod() throws SQLException {
        this.setMessage(Utilities.getLanguages("AdMsg0005"));
        this.setMsgImage("/Administrator/images/alert.png");
        RequestContext.getCurrentInstance().update("msg0001ID");
        RequestContext.getCurrentInstance().execute("PF('msg0001').show()");
    }
    
    public String getMsgImage() {
        return msgImage;
    }
    
    public void setMsgImage(String msgImage) {
        this.msgImage = msgImage;
    }
    
    public String getMessage() {
        return message;
    }
    
    
    public void setMessage(String message) {
        this.message = message;
    }

    // <editor-fold defaultstate="collapsed" desc="Public Method Code">  
    public String getLanguages(String key) throws SQLException {
        return Utilities.getLanguages(key);
    }
    
    public String formatNumber(double number, String format) {
        if(number==0)
            return"";
        return Utilities.formatNumber(format, number);
    }
    
    public void redirect(String url) {
        Utilities.redirect(url);
    }
    
    public String parseDate(Date date, String format) {
        Calendar cal=Calendar.getInstance();
        cal.setTime(date);
        if(cal.get(Calendar.YEAR)<2000)
            return"";
        return Utilities.parseDateTime(date, format);
    }
    
    public String capitalizeWords(String input) {
        return WordUtils.capitalize(input);
    }
    
    public String getGetPathUserUploadFile() {
        return Utilities.GetPathUserUploadFile();
    }
    
    public String getGetURLUserUploadFile() {
        return Utilities.GetURLUserUploadFile();
    }
    public String getCurrentDate()
    {
         return Utilities.getCurrentDate();
    }
    public void handleFaceBoolEvent(ComponentSystemEvent event) {
        String code = Utilities.getRequest().getParameter("code");
        SocialAPIWrapper socialAPIWrapper = new SocialAPIWrapper();
        String accessToken = socialAPIWrapper.getAccessToken(code);
        socialAPIWrapper.setAccessToken(accessToken);
        JSONObject obj = (JSONObject) socialAPIWrapper.getUserInfo();
        Sys_nguoidung sys_nguoidung = Utilities.getManagedBean(Sys_NguoiDungController.class).getItem();
       
        
        Utilities.redirect("/dang-ky.html");
    }
    
  
    // </editor-fold>
}
